package com.ruoyi.web.controller.business;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.StoreInfo;
import com.ruoyi.business.service.IStoreInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门店管理Controller
 *
 * @author 小陈
 * @date 2022-11-24
 */
@RestController
@RequestMapping("/business/storeInfo")
public class StoreInfoController extends BaseController
{
    @Autowired
    private IStoreInfoService storeInfoService;

    /**
     * 查询门店管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:storeInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreInfo storeInfo)
    {
        startPage();
        List<StoreInfo> list = storeInfoService.selectStoreInfoList(storeInfo);
        return getDataTable(list);
    }

    /**
     * 导出门店管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:storeInfo:export')")
    @Log(title = "门店管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StoreInfo storeInfo)
    {
        List<StoreInfo> list = storeInfoService.selectStoreInfoList(storeInfo);
        ExcelUtil<StoreInfo> util = new ExcelUtil<StoreInfo>(StoreInfo.class);
        util.exportExcel(response, list, "门店管理数据");
    }

    /**
     * 获取门店管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:storeInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(storeInfoService.getById(id));
    }

    /**
     * 新增门店管理
     */
    @PreAuthorize("@ss.hasPermi('business:storeInfo:add')")
    @Log(title = "门店管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StoreInfo storeInfo)
    {
        Long id = SecurityUtils.getUserId();
        storeInfo.setCreatedTime(new Date());
        storeInfo.setManagerId(id);
        storeInfo.setCreatorId(id);
        storeInfo.setUpdatorId(id);
        return toAjax(storeInfoService.save(storeInfo));
    }

    /**
     * 修改门店管理
     */
    @PreAuthorize("@ss.hasPermi('business:storeInfo:edit')")
    @Log(title = "门店管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StoreInfo storeInfo)
    {

        storeInfoService.update(storeInfo);
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除门店管理
     */
    @PreAuthorize("@ss.hasPermi('business:storeInfo:remove')")
    @Log(title = "门店管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids, Integer status)
    {

        if (StoreInfo.STORE_INFO_DEAD.equals(status)) {
                storeInfoService.restore(ids);
        } else {
            storeInfoService.removeBatchByIds(ids);
        }
        return AjaxResult.success("修改成功");
    }
}
